package cn.j0n4than.api.interceptor;

import cn.j0n4than.api.utils.DesUtil;
import cn.j0n4than.api.utils.Response;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 基拦截器
 *
 * @author jonathan
 */
public class BaseInterceptor implements HandlerInterceptor {

    protected final ObjectMapper mapper = new ObjectMapper();
    protected final DesUtil util = new DesUtil();

    /**
     * 未授权方法
     *
     * @param response HttpServletResponse
     * @return bool
     */
    protected boolean unauthorized(HttpServletResponse response) {
        try {
            String json = mapper.writeValueAsString(Response.error().setMessage("unauthorized").setStatus(401));
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            response.setStatus(401);

            response.getWriter().write(json);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}
